﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization;
using System.Threading;
using Grundy.Entity;
using System.Linq;
using Grundy.Message;

namespace Grundy.Licensing.Server
{
    [DataContract]
    
    public class ExecutionCount
    {
       public Guid Id { get; set; }

        public ExecutionCount(FeatureIdentificationKey featureIdentificationKey, long limit, IEnumerable<ClientSession> clientSessions)
        {
            Id = Guid.NewGuid();
            FeatureIdentificationKey = featureIdentificationKey;
            Executions = limit;
            ClientSessions = (clientSessions ?? new List<ClientSession>()).ToList();
        }

        public ExecutionCount()
        {
            Id = Guid.NewGuid();
            FeatureIdentificationKey = FeatureIdentificationKey.Null;
            Executions = 0;
            ClientSessions = new List<ClientSession>();
        }

        public void Add(ClientSession clientSession)
        {
            ClientSessions.Add(clientSession);
            Interlocked.Increment(ref _executions);
        }

        [DataMember]
        public FeatureIdentificationKey FeatureIdentificationKey { get; internal set; }

        private long _executions;
        [DataMember]
        public long Executions
        {
            get { return _executions; }
            internal set { _executions = value; }
        }

        [DataMember]
        public List<ClientSession> ClientSessions { get; internal set; }
    }
}